package model;

import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import base.BaseModel;
import base.BaseObserver;
import base.ResultCallBack;
import bean.InfoSchoolBean;
import bean.InfoSchoolBean2;
import bean.InfoSchoolBean3;
import bean.ResultBean;
import jiyun.com.yiyanmeng.http.HttpManager;
import jiyun.com.yiyanmeng.http.MySerivce;
import okhttp3.ResponseBody;
import utils.RxUtil;

/**
 * Created by Windows on 2019/11/5.
 */

public class EditInfoModel extends BaseModel {
    public void requestEditInfo_school(final ResultCallBack<List<InfoSchoolBean>> resultCallBack) {
        MySerivce serivce = HttpManager.getInstance().getSerivce();
        serivce.editinfo_school()
                .compose(RxUtil.<ResultBean<List<InfoSchoolBean>>>changeThread())
                .compose(RxUtil.<List<InfoSchoolBean>>changeResult())
                .subscribe(new BaseObserver<List<InfoSchoolBean>>() {
                    @Override
                    public void onSuccess(List<InfoSchoolBean> infoschoolBeans) {
                        resultCallBack.onSuccess(infoschoolBeans);
                    }

                    @Override
                    public void onFail(String error) {
                        resultCallBack.onFail(error);
                    }
                });

    }
    public void requestEditInfo_time(final ResultCallBack<List<String>> resultCallBack) {
        MySerivce serivce = HttpManager.getInstance().getSerivce();
        serivce.editinfo_time()
                .compose(RxUtil.<ResultBean<List<String>>>changeThread())
                .compose(RxUtil.<List<String>>changeResult())
                .subscribe(new BaseObserver<List<String>>() {
                    @Override
                    public void onSuccess(List<String> timeBean) {
                        resultCallBack.onSuccess(timeBean);
                    }

                    @Override
                    public void onFail(String error) {
                        resultCallBack.onFail(error);
                    }
                });

    }

    public void requestEditInfo_school2(final ResultCallBack<InfoSchoolBean2> resultCallBack, String id) {
        MySerivce serivce = HttpManager.getInstance().getSerivce();
        serivce.editinfo_school2(id)
                .compose(RxUtil.<ResultBean<InfoSchoolBean2>>changeThread())
                .compose(RxUtil.<InfoSchoolBean2>changeResult())
                .subscribe(new BaseObserver<InfoSchoolBean2>() {
                    @Override
                    public void onSuccess(InfoSchoolBean2 infoSchoolBean2s) {
                        resultCallBack.onSuccess(infoSchoolBean2s);
                    }

                    @Override
                    public void onFail(String error) {
                        resultCallBack.onFail(error);
                    }
                });
    }
    public void requestEditInfo_school3(final ResultCallBack<List<InfoSchoolBean3>> resultCallBack, String id, String id2) {
        Log.i("liuzhen", "onSuccess: "+"id:-------"+"   "+id+"id2:---------"+id2);
        MySerivce serivce = HttpManager.getInstance().getSerivce();
        serivce.editinfo_school3(id,id2).compose(RxUtil.<ResponseBody>changeThread())
                .subscribe(new BaseObserver<ResponseBody>() {
                    @Override
                    public void onSuccess(ResponseBody responseBody) {
                        List<InfoSchoolBean3> list=new ArrayList<>();
                        try {
                            String json = responseBody.string();
                            Log.i("liuzhen", "onSuccess: "+json);
                            JSONObject jsonObject = new JSONObject(json);
                            JSONObject info = jsonObject.getJSONObject("info");
                            Iterator<String> keys = info.keys();
                            while (keys.hasNext()){
                                String next = keys.next();
                                JSONObject jsonObject1 = info.getJSONObject(next);
                                String id = (String) jsonObject1.get("id");
                                String zhuan_name = (String) jsonObject1.get("zhuan_name");
                                String zhuan_pid = (String) jsonObject1.get("zhuan_pid");
                                Log.i("qn", "onSuccess: "+id);
                                Log.i("qn", "onSuccess: "+zhuan_name);
                                Log.i("qn", "onSuccess: "+zhuan_pid);
                                InfoSchoolBean3 infoSchoolBean3 = new InfoSchoolBean3(id, zhuan_name, zhuan_pid);
                                list.add(infoSchoolBean3);
                            }
                            resultCallBack.onSuccess(list);
                        } catch (IOException e) {
                            e.printStackTrace();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onFail(String error) {
                    }
                });

    }
    public void requestEditInfo_submit(final ResultCallBack<Void> resultCallBack, String id1, String id2,String id3,String time) {
        Log.i("qn", "onSuccess: "+"id1:-------"+"   "+id1+"id2:---------"+id2+"   "+"id3:---------"+id3);
        MySerivce serivce = HttpManager.getInstance().getSerivce();
        serivce.editinfo_submit(id1,id2,id3,time)
                .compose(RxUtil.<ResponseBody>changeThread())
                .subscribe(new BaseObserver<ResponseBody>() {
                    @Override
                    public void onSuccess(ResponseBody responseBody) {
                        try {
                         String json = responseBody.string();
                            Log.i("qn", "onSuccess: "+json);
                            JSONObject jsonObject = new JSONObject(json);
                           if(jsonObject.getString("ret").equals("200")){
                               resultCallBack.onSuccess(null);
                           }else{
                               resultCallBack.onFail(jsonObject.getString("mas"));
                           }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onFail(String error) {
                        resultCallBack.onFail(error);
                    }
                });

    }
}
